import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import './../widget/myDialog.dart';

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  _alertDialog() async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("提示信息"),
          content: const Text("您确定要删除吗"),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop("ok");
              },
              child: const Text("确定"),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop("cancel");
              },
              child: const Text("取消"),
            ),
          ],
        );
      },
    );
    print(result);
  }

  _simpleDialog() async {
    var result = await showDialog(
      barrierDismissible: false, // 表示点击灰色背景的时候是否消失弹出框
      context: context,
      builder: (context) {
        return SimpleDialog(
          title: const Text("请选择语言"),
          children: [
            SimpleDialogOption(
              onPressed: () {
                print("汉语");
                Navigator.pop(context, "汉语");
              },
              child: const Text("汉语"),
            ),
            const Divider(),
            SimpleDialogOption(
              onPressed: () {
                print("英语");
                Navigator.pop(context, "英语");
              },
              child: const Text("英语"),
            ),
            SimpleDialogOption(
              onPressed: () {
                print("日语");
                Navigator.pop(context, "日语");
              },
              child: const Text("日语"),
            ),
          ],
        );
      },
    );
    print("====");
    print(result);
  }

  _modelBottomSheet() async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 300,
          child: Column(
            children: [
              ListTile(
                title: const Text("分享"),
                onTap: () {
                  Navigator.pop(context, "分享");
                },
              ),
              const Divider(),
              ListTile(
                title: const Text("收藏"),
                onTap: () {
                  Navigator.pop(context, "收藏");
                },
              ),
              const Divider(),
              ListTile(
                title: const Text("取消"),
                onTap: () {
                  Navigator.pop(context, "取消");
                },
              ),
              const Divider(),
            ],
          ),
        );
      },
    );
    print(result);
  }

  _toast() {
    Fluttertoast.showToast(
      msg: "This is Center Short Toast",
      toastLength: Toast.LENGTH_SHORT, // 提示时间 针对android
      gravity: ToastGravity.CENTER, // 位置
      timeInSecForIosWeb: 1, // 提示时间 针对ios和web
      backgroundColor: Colors.red, // 背景色
      textColor: Colors.white, // 文本颜色
      fontSize: 16.0,
    );
  }

  _myDialog() async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return MyDialog(
          title: "提示",
          content: "我是一个内容",
          onClose: () {
            print("onClose");
            Navigator.pop(context, "close");
          },
        );
      },
    );
    print(result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Dialog")),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _alertDialog,
              child: const Text("alert弹出框_alertDialog"),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _simpleDialog,
              child: const Text("select弹出框_simpleDialog"),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _modelBottomSheet,
              child: const Text("_modelBottomSheet"),
            ),
            const SizedBox(height: 20),
            ElevatedButton(onPressed: _toast, child: const Text("_toast")),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _myDialog,
              child: const Text("_myDialog"),
            ),
            const SizedBox(height: 20),
          ],
        ),
      ),
    );
  }
}
